<template>
  <div class="container">
    <el-card>
      <div style="float: left">
        <el-form inline>
          <el-form-item label="学科名称">
            <el-input style="width: 200px" v-model="search"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="reset">清除</el-button>
            <el-button type="primary" @click="serchSubject">搜索</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div style="float: right">
        <el-button
          type="success"
          icon="el-icon-edit"
          @click="dialogFormVisible = true"
          >新增学科</el-button
        >
      </div>
      <el-alert
        :title="`数据一共${total}条`"
        type="info"
        show-icon
        :closable="false"
        style="margin: 0 0 15px"
      >
      </el-alert>
      <template>
        <el-table :data="list">
          <el-table-column prop="date" label="序号" type="index" width="120px">
          </el-table-column>
          <el-table-column prop="subjectName" label="学科名称" width="120px">
          </el-table-column>
          <el-table-column prop="username" label="创建者" width="120px">
          </el-table-column>
          <el-table-column prop="addDate" width="200" label="创建日期">
            <template slot-scope="{ row }">
              {{ row.addDate | parseTimeByString }}
            </template>
          </el-table-column>
          <el-table-column
            prop="isFrontDisplay"
            label="前台是否显示"
            width="180px"
            :formatter="showState"
          >
          </el-table-column>
          <el-table-column
            prop="twoLevelDirectory"
            label="二级目录"
            width="200px"
          >
          </el-table-column>
          <el-table-column prop="tags" label="标签" width="200px">
          </el-table-column>
          <el-table-column prop="totals" label="题目数量" width="120px">
          </el-table-column>
          <el-table-column prop="address" label="操作">
            <template slot-scope="{ row }">
              <el-button
                type="text"
                @click="
                  $router.push({
                    path: './directorys',
                    query: { name: row.subjectName, id: row.id },
                  })
                "
                >学科分类</el-button
              >
              <el-button
                type="text"
                @click="
                  $router.push({
                    path: './tags',
                    query: { name: row.subjectName, id: row.id },
                  })
                "
                >学科标签</el-button
              >
              <el-button type="text" @click="editSubject(row)">修改</el-button>
              <el-button type="text" @click="removeSubject(row.id)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </template>
      <!-- 分页 -->
      <el-row type="flex" justify="center">
        <el-col :span="6">
          <div class="block">
            <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page.sync="currentPage"
              :page-sizes="[5, 10, 20, 50]"
              :page-size="pagesize"
              layout=" prev, pager, next ,sizes, jumper"
              :total="total"
              background
            >
            </el-pagination></div
        ></el-col>
      </el-row>
      <!-- 新增弹窗 -->
      <el-dialog
        :title="showTitle"
        :visible.sync="dialogFormVisible"
        width="30%"
        center
      >
        <el-form :model="form" ref="addSubject" :rules="rules">
          <el-form-item
            label="学科名称"
            :label-width="formLabelWidth"
            prop="subjectName"
          >
            <el-input
              v-model="form.subjectName"
              autocomplete="off"
              placeholder="请输入学科名称"
            ></el-input>
          </el-form-item>
          <el-form-item label="是否显示" :label-width="formLabelWidth">
            <el-switch
              active-color="#13ce66"
              inactive-color="#ff4949"
              v-model="form.isFrontDisplay"
              :active-value="1"
              :inactive-value="0"
            >
            </el-switch>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="btnCancel">取 消</el-button>
          <el-button type="primary" @click="btnOK">确 定</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { list, add, update, remove, detail } from '@/api/hmmm/subjects.js'

export default {
  data() {
    return {
      search: '',
      list: [],
      currentPage: 1,
      pagesize: 10,
      total: 0,
      dialogTableVisible: false,
      dialogFormVisible: false,
      form: {
        subjectName: '',
        isFrontDisplay: 1,
        id: ''
      },
      formLabelWidth: '120px',
      rules: {
        subjectName: [
          { required: true, message: '学科名称不能为空', triiger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 获取列表
    async getList() {
      const { data } = await list({
        page: this.currentPage,
        pagesize: this.pagesize,
        subjectName: this.search
      })
      this.list = data.items
      this.total = data.counts
    },
    handleSizeChange(list) {
      this.pagesize = list
      this.getList()
    },
    handleCurrentChange(currentPage) {
      this.currentPage = currentPage
      this.getList()
    },
    // 点击确定
    async btnOK() {
      try {
        await this.$refs.addSubject.validate()
        if (!this.form.id) {
          await add(this.form)
        } else {
          await update(this.form)
        }
        this.dialogFormVisible = false
        this.form.id = ''
        this.$refs.addSubject.resetFields()
        this.getList()
        this.form = {
          subjectName: '',
          isFrontDisplay: 1,
          id: ''
        }
      } catch (error) {
        console.log(error)
      }
    },
    // 点击取消
    btnCancel() {
      // 重置原来的数据
      this.form = {
        subjectName: '',
        isFrontDisplay: 1,
        id: ''
      }
      this.$refs.addSubject.resetFields()
      this.dialogFormVisible = false
    },
    // 修改回显数据
    async editSubject(row) {
      const res = await detail({ id: row.id })
      // console.log(row);
      this.form = {
        subjectName: res.data.subjectName,
        isFrontDisplay: res.data.isFrontDisplay,
        id: row.id
      }
      this.dialogFormVisible = true

      // const res = await update(this.form);
      // console.log(id);
    },
    // 删除
    removeSubject(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示')
        .then(() => {
          remove({ id: id })
        })
        .then(() => {
          this.getList()
          this.$message.success('删除成功')
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 搜索
    async serchSubject() {
      const res = await list({
        page: this.currentPage,
        pagesize: this.pagesize,
        subjectName: this.search
      })
      // console.log(res);
      // 将请求的数据渲染到页面中
      this.list = res.data.items
      this.total = res.data.counts
      this.getList()
    },
    reset() {
      this.search = ''
      this.getList()
    },
    showState(row, column, cellValue, index) {
      if (cellValue === 1) {
        return '是'
      }
      return cellValue === 0 ? '否' : '未知'
    }
  },
  created() {
    this.getList()
  },
  computed: {
    showTitle() {
      return this.form.id ? '修改学科' : '新增学科'
    }
  }
}
</script>

<style scoped  >
.block {
  margin-top: 20px;
}
.container {
  padding: 20px;
}
</style>
